from selenium import webdriver
import time
from selenium.webdriver import ActionChains
from utils.joblagoustrUtil import *
from utils.dbUtils import DataBase
import random

file1 = open('file1.txt', 'a', encoding='utf-8')
file2 = open('file2.txt', 'a', encoding='utf-8')

company_sql = 'insert into lagoujob_company (COMPANY_NAME,CITY,AREA,ADDRESS,NATURE,SCALE,BUSINESS,COMPANY_URL,CREATE_TIME)' \
              ' values (%s,%s,%s,%s,%s,%s,%s,%s,now())'

jobrecord_sql = 'insert into lagoujob_record(COMPANY_ID,JOB_NAME,MIN_SALARY,MAX_SALARY,JOB_EXPERIENCE,EDUCATION,RECRUITING_NUMBERS,JOB_INFORMATION,RELEASE_TIME,CREATE_TIME)'\
                    'values (%s,%s,%s,%s,%s,%s,%s,%s,%s,now())'

company_search_sql = 'select id from  lagoujob_company where COMPANY_NAME=%s and COMPANY_URL=%s'

db = DataBase()

def login_get_cookies():
    browser = webdriver.Chrome()
    browser.get("https://passport.lagou.com/login/login.html")
    time.sleep(3)
    username = browser.find_element_by_xpath('/html/body/section/div[2]/div[1]/div[2]/form/div[1]/input')
    password = browser.find_element_by_xpath('/html/body/section/div[2]/div[1]/div[2]/form/div[2]/input')
    submit = browser.find_element_by_xpath('/html/body/section/div[2]/div[1]/div[2]/form/div[5]/input')
    username.send_keys('15757855334')
    password.send_keys('86528936zhuai')
    submit.click()
    time.sleep(10)

    cookie_ = browser.get_cookies()
    browser.close()
    return cookie_


def get_init_browser(cookie):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    browser = webdriver.Chrome(chrome_options=chrome_options)

    browser.get("https://passport.lagou.com/login/login.html")

    # 添加cookies
    for item in cookie:
        # 以字典的方式添加
        browser.add_cookie(item)
    return browser

def go_main_page(browser):
    job_url = 'https://www.lagou.com/jobs/list_java?px=new&city=%E5%85%A8%E5%9B%BD'
    browser.get(job_url)
    #这里进入第一页
    html = browser.find_element_by_xpath("//*").get_attribute("outerHTML")
    # print(html)
    go_campany_page(browser)

    # 这里翻页 翻29页
    for i in range(1, 3):
        nextpage = browser.find_element_by_xpath('//*[@id="s_position_list"]/div[2]/div/span[@class="pager_next "]')
        ActionChains(browser).move_to_element(nextpage).click().perform()
        time.sleep(5)
        go_campany_page(browser)

#分析每个主要页面
def go_campany_page(browser):
    # 15个公司页面
    for i in range(1, 16):
        currenthandle = browser.current_window_handle
        try:
            h = browser.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[' + str(i) + ']/div[1]/div[1]/div[1]/a/h3')
        except Exception as e:
            print("url:" + browser.current_url)
        ActionChains(browser).move_to_element(h).click().perform()

        # cookies = browser.get_cookies()
        # cookie = [item["name"] + "=" + item["value"] for item in cookies]
        # cookie_str = '; '.join(item for item in cookie)
        # file2.write(cookie_str + '\n')

        handles = browser.window_handles  # 获取所有句柄
        browser.switch_to.window(handles[-1])  # 切换到最后一个句柄
        # html = browser.find_element_by_xpath("//*").get_attribute("outerHTML")

        try:
            company_name = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div[@class="company"]').text
            city = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]/a[1]').text.strip()
            area = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]/a[2]').text.strip()
            address = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]').text
            nature = browser.find_element_by_xpath('//*[@id="job_company"]/dd/ul/li/i[@class="icon-glyph-trend"]/parent::li').text
            scale = browser.find_element_by_xpath('//*[@id="job_company"]/dd/ul/li/i[@class="icon-glyph-figure"]/parent::li').text
            business = browser.find_element_by_xpath('//*[@id="job_company"]/dd/ul/li/i[@class="icon-glyph-fourSquare"]/parent::li').text
            company_url = browser.find_element_by_xpath('//*[@id="job_company"]/dd/ul/li/i[@class="icon-glyph-home"]/parent::li/a').text
        except Exception as e:
            print("url:"+browser.current_url)
            html = browser.find_element_by_xpath("//*").get_attribute("outerHTML")
            print(html)

        companyDetail = []
        companyDetail.append(company_name)
        companyDetail.append(city)
        companyDetail.append(area)
        companyDetail.append(address)
        companyDetail.append(nature)
        companyDetail.append(scale)
        companyDetail.append(business)
        companyDetail.append(company_url)
        company_search_condition = []
        company_search_condition.append(company_name)
        company_search_condition.append(company_url)
        record = db.select_one(company_search_sql, tuple(company_search_condition))
        if record is None:
            # 插入
            company_id = db.insert_one(company_sql, tuple(companyDetail))
        else:
            company_id = record[0]

        try:
            #job_record
            date = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/dd/p[2]').text
            job_name = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/span[@class="name"]').text
            salary = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/dd/p[1]/span[1]').text
            experience = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/dd/p[1]/span[3]').text.replace("/","").strip()
            education = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/dd/p[1]/span[4]').text.replace("/","").strip()
            job_detail = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[2]/div').text
        except Exception as e:
            print("url:" + browser.current_url)
            html = browser.find_element_by_xpath("//*").get_attribute("outerHTML")
            print(html)


        jobresult = []
        jobresult.append(company_id)
        jobresult.append(job_name)
        jobresult += salary_deal(salary)
        jobresult.append(experience)
        jobresult.append(education)
        jobresult.append('')  # 招聘人数 无
        jobresult.append(job_detail)
        jobresult.append(time.strftime("%Y-%m-%d"))
        db.insert_one(jobrecord_sql, tuple(jobresult))

        time.sleep(random.randint(3,8))
        browser.close()
        browser.switch_to.window(currenthandle)


if __name__ == '__main__':
    browser = get_init_browser(login_get_cookies())
    while 1:
        go_main_page(browser)
        time.sleep(600)

    # browser = webdriver.Chrome()
    # browser.get("https://www.lagou.com/jobs/5593711.html")
    # city = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]/a[1]').text.strip()
    # address = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]/a[2]').text
    # a = browser.find_element_by_xpath('//*[@id="job_detail"]/dd[3]/div[1]').text
    # print(city)


